package com.java.atm.Client;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client
{
    private frontEnd frontEnd;
    private Socket socket;
    private PrintWriter out;
    private BufferedReader in;
    private Scanner scanner;

    public Client()
    {
        try
        {
            socket = new Socket("localhost", 8080);
            out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            frontEnd = new frontEnd();
            scanner = new Scanner(System.in);
        }catch(IOException e)
        {

        }

    }

    public void start()
    {
        try
        {
            int opt = frontEnd.printMenu();

            int serverAck = 0;
            while(opt != 6)
            {
                out.println(opt);
                //等待服务器应答
                while(serverAck == 0)
                {
                    serverAck = in.read();
                }

                //获得服务器应答码
                switch(serverAck)
                {
                    case 1://登录
                        System.out.println("请输入用户编号: ");
                        out.println(scanner.nextLine());
                        System.out.println("请输入密码: ");
                        out.println(scanner.nextLine());
                        //等待服务器验证
                        while(serverAck == 1)
                        {
                            serverAck = in.read();
                        }
                        if(serverAck == 11)
                        {
                            System.out.println("登录成功");
                            frontEnd.setLogin(true);
                            opt = frontEnd.printMenu();
                        }else if(serverAck == 10)
                        {
                            System.err.println("登录失败");
                            opt = frontEnd.printMenu();
                        }
                        serverAck = 0;
                        break;
                    case 2://注册
                        System.out.println("请输入用户名: ");
                        out.println(scanner.nextLine());
                        System.out.println("请输入密码: ");
                        String pass1 = scanner.nextLine();
                        System.out.println("请再次输入密码： ");
                        String pass2 = scanner.nextLine();

                        while (!pass1.equals(pass2))
                        {
                            System.out.println("两次密码不同!");
                            System.out.print("请输入密码: ");
                            pass1 = scanner.nextLine();
                            System.out.print("请再次输入密码: ");
                            pass2 = scanner.nextLine();

                        }
                        out.println(pass1);
                        while(serverAck == 2)
                        {
                            serverAck = in.read();
                        }
                        if(serverAck == 21)
                        {
                            System.out.println("注册成功");
                            String idMsg = null;
                            while(idMsg.equals(null))
                            {
                                idMsg = in.readLine();
                            }
                            System.out.println(idMsg);
                            opt = frontEnd.printMenu();
                        }else
                        {
                            System.out.println("注册失败");
                            opt = frontEnd.printMenu();
                        }
                        serverAck = 0;
                        break;
                    case 3:
                        System.out.println("请输入金额: $");
                        double amount = scanner.nextDouble();
                        while(amount <= 0)
                        {
                            System.out.println("非法输入, 请重试");
                            System.out.println("请输入金额: $");
                            amount = scanner.nextDouble();

                        }
                        out.println(amount);
                        while(serverAck == 3)
                        {
                            serverAck = in.read();
                        }
                        if(serverAck == 31)
                        {
                            System.out.println("存款成功");
                            opt = frontEnd.printMenu();
                        }else if(serverAck == 30)
                        {
                            System.out.println("存款失败");
                            opt = frontEnd.printMenu();
                        }
                        serverAck = 0;
                        break;
                    case 4:
                        System.out.println("请输入金额: $");
                        amount = scanner.nextDouble();
                        while(amount <= 0)
                        {
                            System.out.println("非法输入, 请重试");
                            System.out.println("请输入金额: $");
                            amount = scanner.nextDouble();

                        }
                        out.println(amount);
                        while(serverAck == 4)
                        {
                            serverAck = in.read();
                        }
                        if(serverAck == 41)
                        {
                            System.out.println("取款成功");
                            opt = frontEnd.printMenu();
                        }else if(serverAck == 40)
                        {
                            System.out.println("取款失败 余额不足");
                            opt = frontEnd.printMenu();
                        }
                        serverAck = 0;
                        break;



                }
            }

        }catch(Exception e)
        {
            e.getStackTrace();
        }

    }
}
